package com.flow.service.impl;

import java.util.List;
import com.flow.dao.IPermissionMenuDao;
import com.flow.model.PagerModel;
import com.flow.model.PermissionMenu;
import com.flow.service.IPermissionMenuService;
import com.flow.tools.StringTools;

/**
 * 角色菜单服务层
 * 
 * @author wang
 * @2015-12-26 PermissionMenuService.java
 * @param <T>
 * 
 */
public class PermissionMenuService<T> implements IPermissionMenuService<T> {

	/**
	 * Dao层引用
	 */
	private IPermissionMenuDao<T> pmDao;

	/**
	 * 添加
	 */
	@Override
	public boolean insertPermissionMenu(PermissionMenu pm) {

		if (null == pm)
			return false;

		if (!StringTools.nullOrEmpty(pm.getPmName()))
			return false;

		if (!StringTools.nullOrEmpty(pm.getPmNamespace()))
			return false;

		if (!vlidateNamespace(pm.getPmNamespace()))
			return false;

		if (!vlidateName(pm.getPmName()))
			return false;

		if (null == pm.getPmMangerOrplatform())
			return false;

		if (null == pm.getPmStatus())
			return false;

		try {

			if (pmDao.insertPermissionMenu(pm))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	/**
	 * 更新
	 */
	@Override
	public boolean updatePermissionMenu(PermissionMenu pm) {

		if (null == pm)
			return false;

		if (null == pm.getPmId() || 0 == pm.getPmId())
			return false;

		if (!StringTools.nullOrEmpty(pm.getPmName()))
			return false;

		if (!StringTools.nullOrEmpty(pm.getPmNamespace()))
			return false;

		if (null == pm.getPmMangerOrplatform())
			return false;

		if (null == pm.getPmStatus())
			return false;

		try {
			
			if (pmDao.updatePermissionMenu(pm))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;

	}

	/**
	 * 删除
	 */
	@Override
	public boolean deletePermissionMenu(PermissionMenu pm) {

		if (null == pm)
			return false;

		if (null == pm.getPmId() || 0 == pm.getPmId())
			return false;

		try {

			if (pmDao.deletePermissionMenu(pm))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;

	}

	/**
	 * 根据ID删除
	 */
	@Override
	public boolean deletePermissionMenu(String id) {

		if (!StringTools.nullOrEmpty(id))
			return false;

		try {

			String hql = "delete PermissionMenu where pmId = ?";
			if (0 < pmDao.universalMethodPermissionMenu(hql,
					Integer.valueOf(id)))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;

	}

	/**
	 * 根据ID查询
	 */
	@Override
	public PermissionMenu findPermissionMenuById(String id) {

		if (!StringTools.nullOrEmpty(id))
			return null;

		try {

			String hql = "from PermissionMenu where pmId = ?";
			return pmDao.findPermissionMenuByHql(hql, Integer.valueOf(id));

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;

	}

	/**
	 * 查询全部
	 */
	@Override
	public List<T> findAllPermissionMenu() {

		List<T> list = null;

		try {

			String hql = "from PermissionMenu";
			list = pmDao.findPermissionMenuAllByHql(hql);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return list;
	}

	/**
	 * 分页
	 */
	@Override
	public PagerModel findAllPermissionMenuByPager() {

		PagerModel pager = new PagerModel();

		try {

			String hql = "from PermissionMenu";
			pager = pmDao.findPermissionMenuPagerModeled(pager, hql);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return pager;
	}

	/**
	 * 验证namesoace
	 */
	@Override
	public boolean vlidateNamespace(String namespace) {

		if (!StringTools.nullOrEmpty(namespace))
			return false;

		try {

			String hql = "from PermissionMenu where pmNamespace = ?";
			if (null == pmDao.findPermissionMenuByHql(hql, namespace))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	/**
	 * 验证name
	 */
	@Override
	public boolean vlidateName(String name) {

		if (!StringTools.nullOrEmpty(name))
			return false;

		try {

			String hql = "from PermissionMenu where pmName = ?";
			if (null == pmDao.findPermissionMenuByHql(hql, name))
				return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	public void setPmDao(IPermissionMenuDao<T> pmDao) {
		this.pmDao = pmDao;
	}

}
